﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FAS.App_Code;
using System.Net;
using System.IO;

namespace Arquitectura.SEM
{
    public class ValidaSEM
    {
        public static Boolean validar(String loginUsername, String password)
        {
            String url = "http://"+Auxiliar.getIp()+"/validaUsuarioITESM.php?usuario="+loginUsername+"&password="+password;
            Console.WriteLine(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 10000;

            try
            {
                using (WebResponse response = (HttpWebResponse)request.GetResponse())
                {
                      byte[] bytes = ReadFully(response.GetResponseStream());
                      string s = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
                      return s.Equals("1");
                }
            }
            catch (WebException w)
            {
                Console.WriteLine("Error Occured"+w.ToString());
                return false;
            }

        }
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

    }
}
